﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GpuProgramCompiler.Compilers
{
    class ShaderCompilerDX11
    {
        public ShaderCompilerDX11(CmdLineArgs args, ref CompiledShader outCompiledShader)
        {
            Byte[] pixelShaderCode;
            Byte[] vertexShaderCode;
            String[] pixelShaderConstants;
            String[] vertexShaderConstants;

			Helpers.CompileShader(args["IN"], "ps_4_0", "ps_main", null, args["DEBUGINFO"] != null, out pixelShaderCode, out pixelShaderConstants);
			Helpers.CompileShader(args["IN"], "vs_4_0", "vs_main", null, args["DEBUGINFO"] != null, out vertexShaderCode, out vertexShaderConstants);

            if (vertexShaderCode != null)
                outCompiledShader.Programs.Add(OutputShader(vertexShaderCode, vertexShaderConstants, "VertexShader"));

            if (pixelShaderCode != null)
                outCompiledShader.Programs.Add(OutputShader(pixelShaderCode, pixelShaderConstants, "PixelShader"));
        }

        CompiledShader.Program OutputShader(Byte[] shaderCode, String[] shaderConstants, String name)
        {
            MemoryStream memStream = new MemoryStream(1024 * 1024);
            BinaryWriter writer = new BinaryWriter(memStream);

            writer.Write(shaderCode.Length);
            writer.Write(shaderConstants == null ? 0 : shaderConstants.Length);
            writer.Write(shaderCode);

            // Would write constants here

            return new CompiledShader.Program(name, name, memStream.ToArray());
        }
    }
}
